"""
响应格式化工具

提供统一的API响应格式化功能，包括：
- 成功响应格式化
- 错误响应格式化
- HTTP状态码响应
"""

from fastapi import HTTPException, status
from typing import Any, Dict, Optional
from pydantic import BaseModel

class ResponseModel(BaseModel):
    """
    统一响应格式模型
    
    标准API响应格式，包含：
    - success: 操作是否成功
    - message: 响应消息
    - data: 响应数据（可选）
    - error: 错误信息（可选）
    """
    success: bool
    message: str
    data: Optional[Any] = None
    error: Optional[str] = None

def success_response(data: Any = None, message: str = "操作成功") -> Dict:
    """
    成功响应
    
    Args:
        data: 响应数据
        message: 响应消息
        
    Returns:
        Dict: 成功响应字典
    """
    return {
        "success": True,
        "message": message,
        "data": data
    }

def error_response(message: str, error: Optional[str] = None, status_code: int = 400) -> HTTPException:
    """
    错误响应
    
    Args:
        message: 错误消息
        error: 错误详情
        status_code: HTTP状态码
        
    Returns:
        HTTPException: HTTP异常
    """
    raise HTTPException(
        status_code=status_code,
        detail={
            "success": False,
            "message": message,
            "error": error
        }
    )

def not_found_response(message: str = "资源不存在") -> HTTPException:
    """
    404响应
    
    Args:
        message: 错误消息
        
    Returns:
        HTTPException: 404异常
    """
    return error_response(message, status_code=404)

def unauthorized_response(message: str = "未授权访问") -> HTTPException:
    """
    401响应
    
    Args:
        message: 错误消息
        
    Returns:
        HTTPException: 401异常
    """
    return error_response(message, status_code=401)

def forbidden_response(message: str = "权限不足") -> HTTPException:
    """
    403响应
    
    Args:
        message: 错误消息
        
    Returns:
        HTTPException: 403异常
    """
    return error_response(message, status_code=403)